package com.sh.controller;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.sh.common.ResponseInfoEnum;
import com.sh.common.ReturnVO;
import com.sh.entity.User;

/**
 *  客户 相关 controller
 * */
@Controller
public class BaseController {
	@Value("${addr.upload}")
	protected String serverPath;	//服务器文件地址

	public final Logger log = LoggerFactory.getLogger(this.getClass());

	//拿取登录用户
	public User getLoginUser(){

		ServletRequestAttributes attributes=
				(ServletRequestAttributes)RequestContextHolder.getRequestAttributes();

		HttpServletRequest request = attributes.getRequest();
		String loginUserName =
				(String)request.getAttribute("loginUserName");
		User user =  new User();
		user.setUserName(loginUserName);
		return user;
	}


	//返回  业务成功数据
	public ReturnVO succForService(ResponseInfoEnum riEnum,Object data){
		return ReturnVO.succForService(riEnum.getCode(), riEnum.getMessage(), data);
	}
	//返回  成功数据
	public ReturnVO succ(Object data){
		return ReturnVO.succ(data);
	}
	//返回  成功的分页数据
	public ReturnVO succForPage(ResponseInfoEnum riEnum,Object data,int count){
		return ReturnVO.succForPage(riEnum.getCode(), riEnum.getMessage(), data, count);
	}


	//返回 失败的业务信息
	public ReturnVO failForService(String code,ResponseInfoEnum riEnum){
		return ReturnVO.failForService(code,riEnum.getCode(), riEnum.getMessage());
	}
	//返回 失败的业务信息
	public ReturnVO failForService(String code,ResponseInfoEnum riEnum,String msg){
		return ReturnVO.failForService(code,riEnum.getCode(), riEnum.getMessage()+" "+msg);
	}
	//返回失败信息
	public ReturnVO fail(){
		return ReturnVO.fail();
	}


}
